import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken, setNickName } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    nickName: '',
    sex: '',
    userAccount: '',
    userPhone: '',
    userPower: '',
    userType: ''
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_NICKNAME: (state, nickName) => {
    console.log(' --', state.nickName, ' --', nickName)
    state.nickName = nickName
  },
  SET_SEX: (state, sex) => {
    state.sex = sex
  },
  SET_USERACCOUNT: (state, userAccount) => {
    state.userAccount = userAccount
  },
  SET_USERPHONE: (state, userPhone) => {
    state.userPhone = userPhone
  },
  SET_USERPOWER: (state, userPower) => {
    state.userPower = userPower
  },
  SET_USERTYPE: (state, userType) => {
    state.userType = userType
  }
}

const actions = {
  // 用户登录
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ userAccount: username.trim(), userPwd: password }).then(response => {
        const { data } = response
        console.log(data)
        // commit('SET_TOKEN', data.token)
        setToken(data.data)
        console.log(data.data)
        commit('SET_TOKEN', data)
        // setToken(data.token)
        resolve()
        console.log(data)
      }).catch(error => {
        console.log(error)
        reject(error)
      })
    })
  },

  // 用户个人信息
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      const params = { userAccount: state.token }
      console.log(state.token)
      console.log(params)
      getInfo(state.token).then(response => {
        const { data } = response
        console.log(data)
        if (!data) {
          return reject('Verification failed, please Login again.')
        }

        // const { nickName, sex, userAccount, userPhone, userPower, userType } = data
        const { nickName } = data

        console.log('nickName is --', nickName)
        setNickName(nickName)
        commit('SET_NICKNAME', nickName)
        // commit('SET_AVATAR', avatar)
        // commit('SET_SEX', sex)
        // commit('SET_USERACCOUNT', userAccount)
        // commit('SET_USERPHONE', userPhone)
        // commit('SET_USERPOWER', userPower)
        // commit('SET_USERTYPE', userType)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 用户登出
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // 首先移除用户的令牌
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // 首先移除用户的令牌
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

